package gota;

import gate.AnnotationSet;
import gate.Document;
import gate.Factory;
import gate.creole.SerialAnalyserController;
import gate.gui.MainFrame;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;

import java.io.*;
import java.util.ArrayList;
import java.util.Collection;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import sun.awt.Win32GraphicsConfig;

public class GotaWrapper {

    @SuppressWarnings("unchecked")
	public String ProcessRequest(String requestXml) throws Exception {
        StringBuffer buffer = new StringBuffer();

        
        SerialAnalyserController controller = (SerialAnalyserController) Factory.createResource("gate.creole.SerialAnalyserController");
        ResourceLoader loader = new ResourceLoader();
        try {
            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

            org.w3c.dom.Document document = (org.w3c.dom.Document) builder.parse(new ByteArrayInputStream(requestXml.getBytes("UTF-8")));

            loader.init(controller, document);

            loader.LoadResources();

            controller.execute();

            buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            buffer.append(System.getProperty("line.separator"));
            buffer.append("<Results>");
            buffer.append(System.getProperty("line.separator"));
            ArrayList<Document> processedDocuments = loader.getDocuments();
            for (int i = 0; i < processedDocuments.size(); i++) {
                Document processedDoc = processedDocuments.get(i);
                buffer.append("   <Result>");
                buffer.append(System.getProperty("line.separator"));
                buffer.append("      <![CDATA[");
                buffer.append(System.getProperty("line.separator"));
                buffer.append("        <?xml version=\"1.0\" encoding=\"utf-8\" ?>");
                buffer.append(System.getProperty("line.separator"));
                buffer.append("        <Annotations>");
                buffer.append(System.getProperty("line.separator"));
                AnnotationSet annotations = processedDoc.getAnnotations();
                //String s =  processedDoc.toString();
                String documentXml = processedDoc.toXml(annotations);
                buffer.append(documentXml);

                buffer.append(System.getProperty("line.separator"));
                buffer.append("        </Annotations>");
                buffer.append(System.getProperty("line.separator"));
                buffer.append("      ]]>");
                buffer.append(System.getProperty("line.separator"));
                buffer.append("   </Result>");
                buffer.append(System.getProperty("line.separator"));
            }

            buffer.append("</Results>");
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            //Cleanup Processing Resources
            Collection processingResourceCollection = controller.getPRs();
            Object[] processingResourceArray = processingResourceCollection.toArray(new Object[processingResourceCollection.size()]);
            for (int i = processingResourceArray.length - 1; i > -1; i--) {
                try
                {
                gate.Resource res = (gate.Resource) processingResourceArray[i];
                Factory.deleteResource(res);
                }catch(Exception ex){
                    ex.printStackTrace();
                }
            }

            try{
            //Cleanup Pipeline
            Factory.deleteResource(controller);
            //Cleanup Corpus
            Factory.deleteResource(loader.getCorpus());
            
            }catch(Exception ex){
                    ex.printStackTrace();
                }
            //Cleanup Documents
            ArrayList<Document> processedDocuments = loader.getDocuments();
            for(int i = processedDocuments.size() - 1; i > -1 ; i--){
                try {
                Factory.deleteResource(processedDocuments.get(i));
                }catch(Exception ex){
                    ex.printStackTrace();
                }
            }
        }
        return buffer.toString();
    }

    public void ShowGui() {
        MainFrame mainFrame = new MainFrame();
        mainFrame.setSize(1000, 800);
        mainFrame.setVisible(true);
    }
}
